package org.stone.project.tx.config;

import cn.hutool.extra.spring.SpringUtil;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.stone.project.tx.pojo.dto.UserCtxDto;
import org.stone.project.tx.pojo.dto.UuidUserCtxDto;
import org.stone.project.tx.pojo.dto.UuidUserDto;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.Map;

@Component
public class CtxHolderProxy {
    @PostConstruct
    public void init(){
        CtxHolderInterface bean = SpringUtil.getBean(CtxHolderInterface.class);
        CtxHolderProxy.ctxHolderInterface = bean;
    }

    private static CtxHolderInterface ctxHolderInterface;

    /**
     * 该接口抛出，存在安全问题，外部不可修改该数据
     * @return
     */
    public static Map<ChannelHandlerContext, UuidUserDto> getAllCtx() {
        return ctxHolderInterface.getAllCtx();
    }

    public static Integer getConnectionByUserNum(String userId) {
        return ctxHolderInterface.getConnectionByUserNum(userId);
    }

    public static Integer getConnectionNum() {
        return ctxHolderInterface.getConnectionNum();
    }

    public static Integer getConnectionUserNum() {
        return ctxHolderInterface.getConnectionUserNum();
    }

    public static Map<String, UuidUserCtxDto> findByUserIdAll(String userId){
        return ctxHolderInterface.findByUserIdAll(userId);
    }

    public static ChannelHandlerContext findByUserId(String userId,String linkId){
        return ctxHolderInterface.findByUserId(userId,linkId);
    }

    public static UuidUserDto findByCtx(ChannelHandlerContext ctx){
        return ctxHolderInterface.findByCtx(ctx);
    }

    public static void addUserCtx(String userId,String linkId, ChannelHandlerContext ctx){
        ctxHolderInterface.addUserCtx(userId,linkId,ctx);
    }

    public static void removeUserCtx(String userId,String linkId){
        ctxHolderInterface.removeUserCtx(userId,linkId);
    }

    public static UserCtxDto linkRemoveExist(ChannelHandlerContext ctx){
        return ctxHolderInterface.linkRemoveExist(ctx);
    }

    public static UserCtxDto unLinkRemoveExist(ChannelHandlerContext ctx){
        return ctxHolderInterface.unLinkRemoveExist(ctx);
    }
}
